﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Props.Backend;
using Props.AdminWeb.Models;

namespace Props.AdminWeb.Controllers
{
    [Authorize]
    public class ProfileController : BaseController
    {
        public ActionResult Index()
        {
            var profile = new VendorService().GetProfileByName(base.VendorName);

            if (profile == null)
                return RedirectToAction("PageNotFound");

            ProfileIndexModel model = new ProfileIndexModel()
            {
                VendorProfile = profile,
                VendorTitle = profile.Title,
                BackgroundColor = profile.BackgroundColor,
                BackgroundUrl = profile.BackgroundUrl
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult UpdateBasic(string title, string description)
        {
            try
            {
                VendorService service = new VendorService();

                var profile = service.GetProfileByName(VendorName);

                if (profile != null)
                {
                    profile.Title = title;
                    profile.Description = description;

                    service.SaveProfile(profile);
                }

                return Json(new { hasError = false, message = "Datos Guardados." });
            }
            catch
            {
                return Json(new { hasError = true, message = "Ocurrió un error al guardar los datos." });
            }
        }

        [HttpPost]
        public ActionResult AddNewAddress(string value)
        {
            try
            {
                VendorService service = new VendorService();

                var profile = service.GetProfileByName(VendorName);

                if (profile != null)
                {
                    profile.Addresses = profile.Addresses.Concat(new string[] { value }).ToArray();
                    service.SaveProfile(profile);
                }

                return Json(new { hasError = false, message = "Datos actualizados." });
            }
            catch
            {
                return Json(new { hasError = true, message = "Ocurrió un error al guardar los datos." });
            }
        }

        [HttpPost]
        public ActionResult RemoveAddress(string value)
        {
            try
            {
                VendorService service = new VendorService();

                var profile = service.GetProfileByName(VendorName);

                if (profile != null)
                {
                    var addresses = profile.Addresses.ToList();

                    addresses.RemoveAt(addresses.IndexOf(value));

                    profile.Addresses = addresses.ToArray();

                    service.SaveProfile(profile);

                }

                return Json(new { hasError = false, message = "Datos actualizados." });
            }
            catch
            {
                return Json(new { hasError = true, message = "Ocurrió un error al guardar los datos." });
            }
        }
        
        [HttpPost]
        public ActionResult AddNewPhone(string value)
        {
            try
            {
                VendorService service = new VendorService();

                var profile = service.GetProfileByName(VendorName);

                if (profile != null)
                {
                    profile.Phones = profile.Phones.Concat(new string[] { value }).ToArray();
                    service.SaveProfile(profile);
                }

                return Json(new { hasError = false, message = "Datos actualizados." });
            }
            catch
            {
                return Json(new { hasError = true, message = "Ocurrió un error al guardar los datos." });
            }
        }

        [HttpPost]
        public ActionResult RemovePhone(string value)
        {
            try
            {
                VendorService service = new VendorService();

                var profile = service.GetProfileByName(VendorName);

                if (profile != null)
                {
                    var phones = profile.Phones.ToList();

                    phones.RemoveAt(phones.IndexOf(value));

                    profile.Phones = phones.ToArray();

                    service.SaveProfile(profile);

                }

                return Json(new { hasError = false, message = "Datos actualizados." });
            }
            catch
            {
                return Json(new { hasError = true, message = "Ocurrió un error al guardar los datos." });
            }
        }

        [HttpPost]
        public ActionResult AddNewEmail(string value)
        {
            try
            {
                VendorService service = new VendorService();

                var profile = service.GetProfileByName(VendorName);

                if (profile != null)
                {
                    profile.Emails = profile.Emails.Concat(new string[] { value }).ToArray();
                    service.SaveProfile(profile);
                }

                return Json(new { hasError = false, message = "Datos actualizados." });
            }
            catch
            {
                return Json(new { hasError = true, message = "Ocurrió un error al guardar los datos." });
            }
        }

        [HttpPost]
        public ActionResult RemoveEmail(string value)
        {
            try
            {
                VendorService service = new VendorService();

                var profile = service.GetProfileByName(VendorName);

                if (profile != null)
                {
                    var emails = profile.Emails.ToList();

                    emails.RemoveAt(emails.IndexOf(value));

                    profile.Emails = emails.ToArray();

                    service.SaveProfile(profile);

                }

                return Json(new { hasError = false, message = "Datos actualizados." });
            }
            catch
            {
                return Json(new { hasError = true, message = "Ocurrió un error al guardar los datos." });
            }
        }

        [HttpPost]
        public ActionResult AddNewNetwork(string value)
        {
            try
            {
                VendorService service = new VendorService();

                var profile = service.GetProfileByName(VendorName);

                if (profile != null)
                {
                    profile.Networks = profile.Networks.Concat(new string[] { value }).ToArray();
                    service.SaveProfile(profile);
                }

                return Json(new { hasError = false, message = "Datos actualizados." });
            }
            catch
            {
                return Json(new { hasError = true, message = "Ocurrió un error al guardar los datos." });
            }
        }

        [HttpPost]
        public ActionResult RemoveNetwork(string value)
        {
            try
            {
                VendorService service = new VendorService();

                var profile = service.GetProfileByName(VendorName);

                if (profile != null)
                {
                    var networks = profile.Networks.ToList();

                    networks.RemoveAt(networks.IndexOf(value));

                    profile.Networks = networks.ToArray();

                    service.SaveProfile(profile);

                }

                return Json(new { hasError = false, message = "Datos actualizados." });
            }
            catch
            {
                return Json(new { hasError = true, message = "Ocurrió un error al guardar los datos." });
            }
        }
    }
}
